<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/5/12
 * Time: 18:06
 */

namespace app\job;

use think\queue\Job;
use app\api\model\ZhChat as ZhChatModel;
use app\api\model\ZhChatLog as ZhChatLogModel;

class Job1
{
    public function fire(Job $job, $data){
        
        //....这里执行具体的任务
//
        $isJobDone = $this->doTask($data);
        file_put_contents('chat.txt',$isJobDone);
//
        if ($isJobDone) {
            // 成功删除任务
            $job->delete();
        }else{
            // 此任务超过3次则三次
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了


                $job->delete();
            }

            // 不成功延迟2发再次发布
            $job->release(2);
//            $job->delete();
        }
        //如果任务执行成功后 记得删除任务，不然这个任务会重复执行，直到达到最大重试次数后失败后，执行failed方法
//        $job->delete();
//        $delay = 5;
        // 也可以重新发布这个任务
        //$job->release($delay); //$delay为延迟时间
        
    }
    
    
    
    /**
     * 执行逻辑
     * @param $data 数据
     * @return bool 返回结果
     */
    public function doTask($data)
    {
        file_put_contents('chat.txt', '走到这里');
//        print_r($data);
        // 执行数据写入到数据库或逻辑处理
        $map1 = [
            ['wxapp_id', '=' ,$data['wxapp_id']],
            ['u_id', '=' ,$data['data']['msg']['suserinfo']],
            ['s_id', '=' ,$data['data']['msg']['userinfo']['uid']]
        ];
        $map2 = [
            ['wxapp_id', '=' ,$data['wxapp_id']],
            ['u_id', '=' ,$data['data']['msg']['userinfo']['uid']],
            ['s_id', '=' ,$data['data']['msg']['suserinfo']]
        ];
        $findData = ZhChatModel::whereOr([$map1,$map2])->find();
        $a = ZhChatModel::getLastSql();
        if ($findData) {
            if ($findData['status'] == 0) {
                ZhChatModel::where([
                    'id'       => $findData['id'],
                    'wxapp_id' => $findData['wxapp_id']
                ])->update(['status' => 1]);
            }
            $chatId = $findData['id'];
        } else {
            $params = [
                'wxapp_id'    => $data['wxapp_id'],
                'u_id'        => $data['data']['msg']['userinfo']['uid'],
                's_id'        => $data['data']['msg']['suserinfo'],
                'create_time' => date('Y-m-d H:i:s', time()),
                'status'      => 1
            ];
            $chatId = ZhChatModel::insertGetId($params);
        }
        
        if ($chatId) {
            if($data['data']['msg']['type'] == 'text'){
//                print_r(1);
                $arr = [
                    'wxapp_id'   => $data['wxapp_id'],
                    'chat_id'    => $chatId,
                    'type'       => $data['data']['msg']['type'],
                    'content'    => $data['data']['msg']['content']['text'],
                    'add_time'   => date('Y-m-d H:i:s', time()),
                    'u_id'       => $data['userUid'],
                    'record_time'=> $data['data']['msg']['time'],
//                    'is_read'    => $data['id_read']
                ];
            }else{
                
                $arr = [
                    'wxapp_id'   => $data['wxapp_id'],
                    'chat_id'    => $chatId,
                    'type'       => $data['data']['msg']['type'],
                    'content'    => json_encode($data['data']['msg']['content']),
                    'add_time'   => date('Y-m-d H:i:s', time()),
                    'u_id'       => $data['userUid'],
                    'record_time'=> $data['data']['msg']['time'],
//                    'is_read'    => $data['id_read']
                ];
            }
            
            $res = ZhChatLogModel::insertGetId($arr);
//            print_r($res);
            file_put_contents('chat.txt', $res . 'lll');
            if ($res) {
                return true;
            } else {
                return false;
            }
            
        } else {
            return false;
        }
    }
    
    public function failed($data){
        
        // ...任务达到最大重试次数后，失败了
    }
}